﻿using AutoMapper;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Car
{
    public class VehicleAppService : ApplicationService, IVehicleAppService
    {
        private readonly IRepository<Vehicle,int> _vehicleRepository;
        private readonly IVehicleRepository _IVehicleRepository;
        public VehicleAppService(IRepository<Vehicle, int> vehicleRepository, IVehicleRepository IVehicleRepository)
        {
            _vehicleRepository = vehicleRepository;
            _IVehicleRepository = IVehicleRepository;
        }
        /// <summary>
        /// 基础车辆添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public async Task<VehicleDto> Create(VehicleDto m)
        {
            var Vehicle = new Vehicle
            {
                Vmodel=m.Vmodel,
                Vmark=m.Vmark,
                Ownername=m.Ownername,
                Fieldname= m.Fieldname,
                Vlong=m.Vlong,
                Vwide=m.Vwide,
                Vhigh=m.Vhigh,
                Vcolor=m.Vcolor,
                Vpurchase=m.Vpurchase,
                Voperation=m.Voperation,
                Vexpire=m.Vexpire,
                Vannual=m.Vannual,
                Vmaintain=m.Vmaintain
            };

            await _IVehicleRepository.Create(Vehicle);
            return ObjectMapper.Map<Vehicle, VehicleDto>(Vehicle);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _IVehicleRepository.Delete(id);
        }
        
        public async Task<VehicleDto> GetCarryById(int Id)
        {
            var student = await _IVehicleRepository.GetCarryById(Id);
            return ObjectMapper.Map<Vehicle, VehicleDto>(student);
        }

        /// <summary>
        /// 基本显示里面的车辆显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<VehicleDto>> GetListAsync(GetVehicleDto dto)
        {
            //var all = _vehicleRepository.ToList();
            var vehiclelist = await _vehicleRepository.GetPagedListAsync(
                dto.SkipCount,
                dto.MaxResultCount,
                dto.Sorting);
            var totalCount = await AsyncExecuter.CountAsync(
                _vehicleRepository.WhereIf(
                    !dto.Filter.IsNullOrWhiteSpace(),
                    m => m.Vmodel.Contains(dto.Filter)
                )
            );

            return new PagedResultDto<VehicleDto>(
              totalCount,
              ObjectMapper.Map<List<Vehicle>, List<VehicleDto>>(vehiclelist)
          );
        }

        public async Task<int> Update(VehicleDto m)
        {
           var carr= await _vehicleRepository.GetAsync(m.Id);
            carr.Vmodel = m.Vmodel;
            carr.Vmark = m.Vmark;
            carr.Ownername = m.Ownername;
            carr.Fieldname = m.Fieldname;
            carr.Vlong = m.Vlong;
            carr.Vwide = m.Vwide;
            carr.Vcolor = m.Vcolor;
            carr.Vpurchase = m.Vpurchase;
            carr.Voperation = m.Voperation;
            carr.Vexpire = m.Vexpire;
            carr.Vannual = m.Vannual;
            carr.Vmaintain = m.Vmaintain;
            await _vehicleRepository.UpdateAsync(carr);
            return 1;
        }
    }
}
